<?php
/**
 * Created by PhpStorm.
 * User: tao
 * Date: 2016/11/30
 * Time: 下午9:31
 */
namespace Admin\Controller;
use Think\Controller;

class IndexController extends Controller{
    public function index(){
        if(!session('id')){
            $this->display('login');
        }else{
            $this->error('非法操作，请先登录');
        }
    }

    /**
     * 管理员登录
     * 有两个入口，分别是超级管理员/老师和学生
     */
    public function login(){
        if(IS_AJAX){
            $power = I('post.power');
            // 0为管理员或老师，1为学生
            if($power == 0) {
                $adminName = I('post.name');
                $adminPassword =md5(I('post.passWord'));
                if($adminName&&$adminPassword){
                    $map['name']=$adminName;
                    $user = D('user');
                    //查询出的数据的字段都变成了小写，caution
                    $adminInfo = $user->where($map)->find();
                    if($adminInfo){        	
                        if($adminInfo['password'] == $adminPassword){
                            session('adminName',$adminName);
                            session('adminId',$adminInfo['id']);
                            if($adminInfo['power'] == 1){
                                // power=1代表超级管理员
                                $out['status'] = 1;
                            }else{
                                // power=2代表普通管理员
                                $out['status'] = 2;
                            }
                            $out['info'] = '登录成功';
                        }else{
                            $out['info'] = '密码错误，请重新登录';
                            $out['status'] = 0;
                        }
                    }else{
                        $out['info'] = '账号不存在,请重新登录';
                        $out['status'] = 0;
                    }
                }
                $this->ajaxReturn($out);
            }else {
                 $studentname = I('post.name');
                 $idcard= I('post.passWord');
                if($idcard && $studentname) {
                    $map2 = [
                        'idCard'        =>  $idcard,
                        'studentName'   =>  $studentname
                    ];
                    $student = D('student');
                    $stuInfo = $student->where($map2)->find();
                    if($stuInfo) {
                        session('adminName',$studentname);
                        session('adminId',$idcard);
                        $out['info'] = '登录成功！';
                        $out['status'] = 3;
                    }else {
                        $out['info'] = '不好意思握，你没被录取握！\nSorry, you’re not accepted.';
                        $out['status'] = 0;
                    }
                }
                $this->ajaxReturn($out);
            }
        }
        $this->display();
    }
    /**
     * 管理员登出操作，清除session
     */
    public function logout(){
        session('adminName',null);
        session('adminId',null);
        $this->redirect('Index/login');
    }
}